package com.sumainfor.pc.gmio.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sumainfor.common.exception.TCException;
import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.CustomMap;
import com.sumainfor.common.util.DateUtils;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.pc.gmio.dao.BillReviewMapper;
import com.sumainfor.pc.gmio.feign.BaseFeignZuul;
import com.sumainfor.pc.gmio.feign.BasePcMktFeign;
import com.sumainfor.pc.gmio.service.BillReviewService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author Jack Chen
 * @Create 2019-01-23 18:00
 * 业主账单审核
 */
@Service
@Transactional(rollbackFor = Exception.class)
@Log4j2
public class BillReviewServiceImpl implements BillReviewService {

    @Autowired
    private BillReviewMapper billReviewMapper;


    @Autowired
    private BaseFeignZuul feignZuul;

    @Autowired
    private BasePcMktFeign mktFeign;

    /**
     * 查询列表数据
     *
     * @param state
     * @param page
     * @return
     */
    @Override
    public JsonResult getTableList(Integer state, Page page, String projectId) {
        Set<String> hIds = null;
        if (StringUtils.isNotBlank(projectId)) {
            Page<Map<String, Object>> hDataList = mktFeign.selectHousingListView(CustomMap.create("current", 1)
                    .put("size", Long.MAX_VALUE)
                    .put("projectId", projectId));
            if (hDataList != null && CollectionUtils.isNotEmpty(hDataList.getRecords())) {
                hIds = hDataList.getRecords().stream().map(item -> MapUtils.getString(item, "HouInfoId")).collect(Collectors.toSet());
            }
        }

        IPage<Map<String, Object>> response = billReviewMapper.selectTableList(page, state, hIds);
        response.setRecords(response.getRecords().stream().map(item -> {
            //转换时间格式
            DateUtils.formatDateByMap(item);
            String ownerId = MapUtils.getString(item, "StemId");
            String createId = MapUtils.getString(item, "CreateType");

            //获取业主的信息
            Map<String, Object> ownerInfo = mktFeign.getOwnerInfoByParams(CustomMap.create("OwId", ownerId));
            if (MapUtils.isEmpty(ownerInfo)) {
                throw new TCException("获取业主信息失败");
            }
            item.put("OwnerPhone", ownerInfo.get("OwPhone"));
            item.put("OwnerName", ownerInfo.get("OwName"));
            //查询发起人
            if (StringUtils.isBlank(createId)) {
                item.put("CreateName", "系统发起");
            } else {
                //查询
                JsonResult<List<Map<String, Object>>> userInfo = feignZuul.getUserInfo(Arrays.asList(createId));
                if (!userInfo.isSuccess()) {
                    throw new TCException(userInfo.getErrorMessage());
                }
                item.put("CreateName", userInfo.getResult().get(0).get("username"));
            }
            String uploadUserId = MapUtils.getString(item, "UploadUserId");
            item.put("UploadUserName", "");
            if (StringUtils.isNotBlank(uploadUserId)) {
                JsonResult<List<Map<String, Object>>> userInfo = feignZuul.getUserInfo(Arrays.asList(uploadUserId));
                if (!userInfo.isSuccess()) {
                    throw new TCException(userInfo.getErrorMessage());
                }
                if (CollectionUtils.isNotEmpty(userInfo.getResult())) {
                    item.put("UploadUserName", userInfo.getResult().get(0).get("username"));
                } else {
                    item.put("UploadUserName", "无");
                }
            }
            DateUtils.formatDateByMap(item);
            return item;
        }).collect(Collectors.toList()));
        return JsonResult.success(response);
    }


    /**
     * 上传支付凭证
     *
     * @param outId
     * @return
     */
    @Override
    public JsonResult uploadPayProof(String outId) {
        int row = billReviewMapper.updateBillOut(outId, ContextUtils.getUserId());
        if (row <= 0) {
            throw new TCException("提交失败");
        }
        String tempId = billReviewMapper.getTempId(outId);
        if (StringUtils.isNotBlank(tempId)) {
            billReviewMapper.updateBillTempOut(tempId);
        }
        return JsonResult.success();
    }

}
